<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Format\UserAssessLogFormat;
use App\Http\Requests\CreateUserAssessLogRequest;
use App\Services\AssessUserService;
use Illuminate\Http\Request;

class AssessUserController extends Controller
{
    protected $assessUserService;

    public function __construct(AssessUserService $assessUserService)
    {
        $this->assessUserService = $assessUserService;
    }

    public function assess(CreateUserAssessLogRequest $createUserAssessLogRequest)
    {
        $params = $createUserAssessLogRequest->input('assess');
        foreach ($params as $param) {
            $format = new UserAssessLogFormat($param);
            $this->assessUserService->assess($format);
        }
        return renderSuccessJson();
    }

    public function assessTemp(Request $request)
    {
        $type = $request->input('type');
        $res = $this->assessUserService->assessTemp($type);
        return renderSuccessJson($res);
    }

    public function getMyAssessDetail(Request $request)
    {
        $userId = $request->input('user_id');
        $tempId = $request->input('assess_template_id');
        $res = $this->assessUserService->getUserAssess($userId, $tempId);
        return renderSuccessJson($res);
    }

    public function getMyAssessList(Request $request)
    {
        $userId = $request->input('user_id');
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('order_type')) {
            $search['order_type'] = $request->input('order_type');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        $res = $this->assessUserService->getUserAssessList($userId, $page, $pageSize, $search);
        return renderSuccessJson($res);
    }
}
